package cn.demoncat.util.hazelcast.config;

import javax.annotation.Resource;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.core.HazelcastInstance;

/**
 * Hazelcast 客户端配置
 * 
 * @author 延晓磊
 *
 * @since 2020年1月2日
 */
@Configuration
public class HzClientConfig {

	@Resource
	private HzProperties hzProperties;

	/**
	 * 配置对象
	 */
	@ConditionalOnMissingBean(name = {"hazelcastClientConfig", "hazelcastConfig"})
	@Bean
	public ClientConfig hazelcastClientConfig() {
		// 创建客户端配置
		ClientConfig config = new ClientConfig();
		// 实例名称：根据Name获取实例
		config.setInstanceName(hzProperties.getInstance());
		// 集群分组：发现相同组名的节点
		config.getGroupConfig().setName(hzProperties.getGroup());
		return config;
	}

	/**
	 * Hazelcast 实例
	 */
	@Bean
	public HazelcastInstance hzInstance(ClientConfig hazelcastClientConfig) {
		// 创建Hazelcast客户端：不处理数据，不维护节点信息
		return HazelcastClient.newHazelcastClient(hazelcastClientConfig);
	}
}
